<template>
  <div v-if="modelValue" class="dialog-overlay" @click.self="cancel">
    <div class="dialog">
      <h3>{{ title }}</h3>
      <div class="input-group">
        <label>描述</label>
        <input 
          v-model="inputValue"
          :placeholder="placeholder"
          @keyup.enter="confirm"
          @keyup.esc="cancel"
          ref="inputRef"
          type="text"
          autocomplete="off"
        />
        <div v-if="videoStore.recentTags.length > 0" class="recent-tags">
          <div class="recent-tags-title">最近使用的标签：</div>
          <div class="recent-tag-list">
            <div 
              v-for="tag in videoStore.recentTags" 
              :key="tag.description"
              class="recent-tag"
              :style="{ borderColor: tag.color }"
              @click="useRecentTag(tag)"
            >
              {{ tag.description }}
            </div>
          </div>
        </div>
      </div>
      <div class="input-group">
        <label>标签颜色</label>
        <div class="color-picker">
          <div 
            v-for="color in colors" 
            :key="color"
            class="color-option"
            :class="{ active: selectedColor === color }"
            :style="{ backgroundColor: color }"
            @click="selectedColor = color"
          ></div>
        </div>
      </div>
      <div class="buttons">
        <button @click="confirm" :disabled="!inputValue.trim()">确定</button>
        <button @click="cancel">取消</button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, watch, nextTick } from 'vue'
import { useVideoStore } from '../stores/videoStore'

const videoStore = useVideoStore()

const props = defineProps({
  modelValue: Boolean,
  title: {
    type: String,
    default: '输入'
  },
  placeholder: {
    type: String,
    default: '输入标签描述'
  },
  initialValue: {
    type: Object,
    default: () => ({
      description: '',
      color: '#f44336'
    })
  }
})

const emit = defineEmits(['update:modelValue', 'confirm', 'cancel'])

const inputValue = ref('')
const inputRef = ref(null)
const selectedColor = ref('#f44336')

const colors = [
  '#f44336', // 红色
  '#e91e63', // 粉色
  '#9c27b0', // 紫色
  '#673ab7', // 深紫色
  '#3f51b5', // 靛蓝色
  '#2196f3', // 蓝色
  '#03a9f4', // 浅蓝色
  '#00bcd4', // 青色
  '#009688', // 蓝绿色
  '#4caf50', // 绿色
  '#8bc34a', // 浅绿色
  '#cddc39', // 酸橙色
  '#ffeb3b', // 黄色
  '#ffc107', // 琥珀色
  '#ff9800', // 橙色
  '#ff5722'  // 深橙色
]

const useRecentTag = (tag) => {
  inputValue.value = tag.description
  selectedColor.value = tag.color
}

watch(() => props.modelValue, async (newVal) => {
  if (newVal) {
    if (props.initialValue) {
      inputValue.value = props.initialValue.description || ''
      selectedColor.value = props.initialValue.color || '#f44336'
    } else {
      inputValue.value = ''
      selectedColor.value = '#f44336'
    }
    await nextTick()
    inputRef.value?.focus()
  }
}, { immediate: true })

const confirm = () => {
  if (!inputValue.value.trim()) return
  emit('confirm', {
    description: inputValue.value.trim(),
    color: selectedColor.value
  })
  emit('update:modelValue', false)
  inputValue.value = ''
  selectedColor.value = '#f44336'
}

const cancel = () => {
  emit('cancel')
  emit('update:modelValue', false)
  inputValue.value = ''
  selectedColor.value = '#f44336'
}
</script>

<style scoped>
.dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.dialog {
  background: white;
  padding: 20px;
  border-radius: 8px;
  min-width: 300px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}

.dialog h3 {
  margin: 0 0 16px 0;
  color: #333;
}

.input-group {
  margin-bottom: 16px;
}

.input-group label {
  display: block;
  margin-bottom: 4px;
  color: #666;
  font-size: 14px;
}

.input-group input {
  width: 100%;
  padding: 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
  background: white;
  transition: border-color 0.2s;
}

.input-group input:focus {
  outline: none;
  border-color: #1976d2;
}

.color-picker {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 8px;
  padding: 8px;
  background: #f5f5f5;
  border-radius: 4px;
}

.color-option {
  width: 24px;
  height: 24px;
  border-radius: 4px;
  cursor: pointer;
  transition: transform 0.2s;
}

.color-option:hover {
  transform: scale(1.1);
}

.color-option.active {
  box-shadow: 0 0 0 2px white, 0 0 0 4px currentColor;
}

.buttons {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 20px;
}

.buttons button {
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: background-color 0.2s;
}

.buttons button:first-child {
  background: #1976d2;
  color: white;
}

.buttons button:first-child:hover:not(:disabled) {
  background: #1565c0;
}

.buttons button:first-child:disabled {
  background: #ccc;
  cursor: not-allowed;
}

.buttons button:last-child {
  background: #f5f5f5;
  color: #333;
}

.buttons button:last-child:hover {
  background: #e0e0e0;
}

.recent-tags {
  margin-top: 8px;
  padding: 8px;
  background: #f5f5f5;
  border-radius: 4px;
}

.recent-tags-title {
  font-size: 12px;
  color: #666;
  margin-bottom: 4px;
}

.recent-tag-list {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.recent-tag {
  padding: 4px 8px;
  background: white;
  border: 2px solid;
  border-radius: 4px;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.2s;
}

.recent-tag:hover {
  transform: translateY(-1px);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
</style> 